package com.hzh.music;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.hzh.common.httpclient.HttpProtocolHandler;
import com.hzh.common.httpclient.HttpRequest;
import com.hzh.common.httpclient.HttpResponse;

public class Net163MusicService implements MusicService {
	public static Net163MusicService SERVICE = new Net163MusicService();
	
	public static String NET163_MUSIC = "http://music.163.com";
	public static String NET163_MUSIC_LIST_URL = NET163_MUSIC + "/playlist?id=";
	public static String NET163_MUSIC_API_URL = NET163_MUSIC + "/api/song/detail/?id=%s&ids=%%5B%s%%5D";
	
	/* (non-Javadoc)
	 * @see com.hzh.music.MusicService#getMusicList(java.lang.String)
	 */
	@Override
	public List<Music> getMusicList(String listId) throws Exception {
		
		if(StringUtils.isBlank(listId)) {
			return Lists.newArrayList();
		}
		
		// 请求
		HttpProtocolHandler handler = HttpProtocolHandler.getInstance();
		HttpRequest request = new HttpRequest();
		request.setUrl(NET163_MUSIC_LIST_URL + listId);
		request.setMethod(HttpRequest.METHOD_GET);
		HttpResponse response = handler.execute(request, null, null, null);
		
		// 解析
		String html = response.getStringResult();		
		Document doc = Jsoup.parse(html);	
		Elements allSong = doc.select("tbody tr");
		
		// 装载
		List<Music> musics = Lists.newArrayList();
		for(int i = 0; i < allSong.size(); i++) {
			Music music = getSongInfo(allSong.get(i).attr("data-id"));				
			musics.add(music);
		}
		return musics;
	}
	
	/* (non-Javadoc)
	 * @see com.hzh.music.MusicService#getSongInfo(java.lang.String)
	 */
	@Override
	public Music getSongInfo(String id) throws Exception {
		
		Music music = Music.DAO.findById(id);
		if(music != null) {
			return music;
		}
		HttpProtocolHandler handler = HttpProtocolHandler.getInstance();
		HttpRequest request = new HttpRequest();
		request.setUrl(String.format(NET163_MUSIC_API_URL, id, id));
		request.setMethod(HttpRequest.METHOD_GET);
		HttpResponse response = handler.execute(request, null, null, null);
		
		// 解析
		music = new Music();
		String html = response.getStringResult();	
		JSONObject musicInfoJson = JSONObject.parseObject(html).getJSONArray("songs").getJSONObject(0);
		
		music.set("id", musicInfoJson.getString("id"));
		music.set("name", musicInfoJson.getString("name"));
		music.set("url", musicInfoJson.getString("mp3Url"));
		music.set("artist", musicInfoJson.getJSONArray("artists").getJSONObject(0).getString("name"));
		music.set("album", musicInfoJson.getJSONObject("album").getString("name"));
		
		// 保存
		music.save();
		return music;
	}

	public static void main(String[] args) throws Exception {
		MusicService service = new Net163MusicService();
		List<Music> musics = service.getMusicList("57500331");
		System.out.println(musics);
		//System.out.println(String.format(NET163_MUSIC_API_URL, "11", "11"));
	}

}
